//
//  MKHomePopAnimation.swift
//  
//
//  Created by 劢克科技 on 2019/1/18.
//

import UIKit

//MARK: - delegate
protocol MKHomePopAnimationDelegate {
    func changeStatus(present: Bool)
}

class MKHomePopAnimation: NSObject {
    var isPresent = true
    var currentFrame = CGRect.zero
    
    var delegate: MKHomePopAnimationDelegate?
}

//MARK: - pop delegate
extension MKHomePopAnimation: UIViewControllerTransitioningDelegate {
    
    /// Change pop size
    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
        
        let presentVc = MKPopPresentationController(presentedViewController: presented, presenting: presenting)
        
        presentVc.currentFrame = currentFrame
        
        return presentVc
    }
    
    /// Change pop animation
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return self
    }
    
    /// Change miss animation
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return self
    }
}

//MARK: - pop animation delegate
extension MKHomePopAnimation : UIViewControllerAnimatedTransitioning {
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.5
    }
    
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        isPresent ? presentAnimation(transitionContext: transitionContext) : dismissAnimation(transitionContext: transitionContext)

        delegate?.changeStatus(present: isPresent)
        
        isPresent = !isPresent
    }
    
    /// present animation
    func presentAnimation(transitionContext: UIViewControllerContextTransitioning) {
        // get presented view
        guard let presentedView = transitionContext.view(forKey: .to) else {
            return
        }
        
        // add sub view
        transitionContext.containerView.addSubview(presentedView)
        
        // add animation
        presentedView.layer.anchorPoint = CGPoint(x: 0.5, y: 0)
        presentedView.transform = CGAffineTransform.init(scaleX: 1, y: 0)
        
        UIView.animate(withDuration: transitionDuration(using: transitionContext)) {
            presentedView.transform = CGAffineTransform.identity
        }
        
        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
            presentedView.transform = CGAffineTransform.identity
        }) { (_) in
            transitionContext.completeTransition(true)
        }
    }
    
    /// dismiss animation
    func dismissAnimation(transitionContext: UIViewControllerContextTransitioning) {
        // get presented view
        guard let presentedView = transitionContext.view(forKey: .from) else {
            return
        }
        
        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
            presentedView.transform = CGAffineTransform.init(scaleX: 1, y: 0.1)
        }) { (_) in
            presentedView.removeFromSuperview()
            transitionContext.completeTransition(true)
        }
        
    }
    
}
